using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Blazing.BusinessObjects.Cms;
using System.Web.UI.HtmlControls;

namespace Blazing.Web.Controls
{
    [Themeable(true)]
    public partial class ImageControl : UserControl
    {
        #region Private Variables
        private bool _link = false;
        private bool _hideNav = false;
        private string _leftarrowImage;
        private string _rightarrowImage;
        private string _middlearrowImage;
        private string _imageStyle;
        CmsManager _manager = null;
        #endregion

        #region Properties
        public CmsManager Manager
        {
            get
            {
                if (_manager == null)
                    _manager = Blazing.Util.CmsManagerHelper.FindManagerObject(Page.Controls);
                if (_manager == null)
                    throw new ApplicationException("There was no active CmsManager object found on the current page.");
                return _manager;
            }
        }
        public bool Link
        {
            get { return _link; }
            set { _link = value; }
        }

        public bool HideNav
        {
            get { return _hideNav; }
            set { _hideNav = value; }
        }

        public string CssClass
        {
            get { return _imageStyle; }
            set { _imageStyle = value; }
        }

        public string LeftArrowImage
        {
            get { return _leftarrowImage; }
            set { _leftarrowImage = value; }
        }

        public string RightArrowImage
        {
            get { return _rightarrowImage; }
            set { _rightarrowImage = value; }
        }

        public string MiddleArrowImage
        {
            get { return _middlearrowImage; }
            set { _middlearrowImage = value; }
        }

        protected int? BuildingId
        {
            get
            {
                return Manager.BuildingId;
            }
        }

        protected int? PageId
        {
            get { return Manager.PageId; }
        }

        protected int? LocationId
        {
            get { return Manager.LocationId; }
        }

        #endregion

        public ImageControl()
        {

        }

        private List<Image> findImages(Contentpage page)
        {
            //List<Image> imageList = new List<Image>();
            //if (PageId > 0)
            //{
            //    ContentPage retpage = ContentPage.Load(PageId.Value);
            //    imageList = (List<Image>)retpage.Images;
            //}
            //return imageList;
            throw new NotImplementedException();
        }

        //private string GetNavTitle(DataObject boundObject)
        //{
            //if (boundObject != null && boundObject is Building)
            //{
            //    Building bld = boundObject as Building;
            //    return bld.Name + ", " + bld.Location.Name;
            //}
            //else if (boundObject != null && boundObject is Location)
            //{
            //    return ((Location)boundObject).Name;
            //}
            //else
            //{
            //    return "Blazing Resorts Group";
            //}
        //    throw new NotImplementedException();
        //}

        protected override void CreateChildControls()
        {
            //this.Controls.Clear();

            //string _navtitle = string.Empty;
            //string arStr = string.Empty;
            //List<Image> imageList = new List<Image>();

            //if (PageId != null)
            //{
            //    ContentPage page = ContentPage.Load(PageId.Value);
            //    imageList = findImages(page);
            //    _navtitle = GetNavTitle(page.BoundObject);
            //}

            ////format page images into javascript array
            //string firstImage = string.Empty;
            //string firstImageAlt = _navtitle;
            //if (imageList.Count > 0)
            //{
            //    firstImage = ((Image)imageList[0]).ImageURL;
            //    firstImageAlt = ((Image)imageList[0]).Alt != string.Empty ? ((Image)imageList[0]).Alt : _navtitle;
            //}

            //foreach (Image wimg in imageList)
            //{
            //    arStr = string.Format("{0}{1}\"{2}\",\"{3}\",\" \"", arStr, arStr.Length > 0 ? "," : "", wimg.ImageURL, wimg.Alt != string.Empty ? wimg.Alt : _navtitle);
            //}


            //HtmlTable _imageNavTable;
            //HtmlGenericControl _imageDiv;
            //System.Web.UI.WebControls.Label _imageName;
            //System.Web.UI.WebControls.Image img;

            ////Main Div
            //_imageDiv = new HtmlGenericControl("div");
            //_imageDiv.ID = "main_image";
            //_imageDiv.Attributes.Add("class", _imageStyle);
            //this.Controls.Add(_imageDiv);

            //HtmlImage resortimg = new HtmlImage();
            //resortimg.Alt = firstImageAlt;
            //resortimg.ID = "resort_image";
            //resortimg.Src = ResolveClientUrl(firstImage);
            //_imageDiv.Controls.Add(resortimg);

            ////Title label
            //_imageName = new System.Web.UI.WebControls.Label();
            //_imageName.ID = "imagename";
            //_imageName.Text = firstImageAlt;
            //_imageName.Visible = !HideNav;

            //if (!_hideNav)
            //{

            //    //Navigation Table
            //    _imageNavTable = new HtmlTable();
            //    _imageNavTable.ID = "imagenav";

            //    HtmlTableRow tr = new HtmlTableRow();

            //    //Arrow Images
            //    HtmlTableCell tc1 = new HtmlTableCell();
            //    tc1.Controls.Add(_imageName);
            //    tr.Cells.Add(tc1);
            //    HtmlTableCell tc2 = new HtmlTableCell();
            //    System.Web.UI.WebControls.HyperLink prev = new System.Web.UI.WebControls.HyperLink();
            //    prev.Attributes.Add("title", "Previous Image");
            //    prev.Attributes.Add("onclick", string.Format("clearInterval(interval);interval=null;loadPreviousImage('{0}');", resortimg.ClientID));
            //    prev.NavigateUrl = "#";

            //    tc2.Controls.Add(prev);
            //    tr.Cells.Add(tc2);

            //    if (!string.IsNullOrEmpty(LeftArrowImage))
            //    {
            //        img = new System.Web.UI.WebControls.Image();
            //        img.AlternateText = "Previous Image";
            //        prev.Controls.Add(img);
            //        img.ImageUrl = Page.ResolveClientUrl(LeftArrowImage);
            //    }


            //    HtmlTableCell tc3 = new HtmlTableCell();
            //    tr.Cells.Add(tc3);
            //    if (!string.IsNullOrEmpty(MiddleArrowImage))
            //    {
            //        img = new System.Web.UI.WebControls.Image();
            //        img.AlternateText = "Picture";
            //        tc3.Controls.Add(img);
            //        img.ImageUrl = Page.ResolveUrl(MiddleArrowImage);
            //    }

            //    HtmlTableCell tc4 = new HtmlTableCell();

            //    System.Web.UI.WebControls.HyperLink next = new System.Web.UI.WebControls.HyperLink();
            //    next.Attributes.Add("title", "Next Image");
            //    next.Attributes.Add("onclick", string.Format("clearInterval(interval);interval=null;loadNextImage('{0}');", resortimg.ClientID));
            //    next.NavigateUrl = "#";
            //    tc4.Controls.Add(next);
            //    tr.Cells.Add(tc4);
            //    if (!string.IsNullOrEmpty(RightArrowImage))
            //    {
            //        img = new System.Web.UI.WebControls.Image();
            //        img.AlternateText = "Next Image";
            //        next.Controls.Add(img);
            //        img.ImageUrl = Page.ResolveClientUrl(RightArrowImage);
            //    }

            //    _imageNavTable.Rows.Add(tr);
            //    _imageDiv.Controls.AddAt(0, _imageNavTable);
            //}

            ////register images for this page
            //Page.ClientScript.RegisterClientScriptBlock(typeof(string), "HeroImageJS",
            //    string.Format("var img = new Array ({0});\nvar txtctrl = \"{1}\";", arStr, _imageName.ClientID), true);
            ////register JS as a webresource
            //Page.ClientScript.RegisterClientScriptInclude("HeroImageJSResource",
            //     Page.ClientScript.GetWebResourceUrl(this.GetType(), "Blazing.Web.Resources.JavaScript.HeroImage.js"));
            throw new NotImplementedException();
        }
    }
}
